<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Vendor_model extends CI_Model{
	function __construct(){
		$this->load->library('mapper/vendor_mapper');
	}
	/**
	 * @return array total data num_results, and number of object return;
	 */
	public function search($condition = array(), $offset = 0, $numOfItemsPerPage = 20){
		//results 
		$results =  $this->vendor_mapper->findBySearchCriteria($condition, $numOfItemsPerPage, $offset);
		//count
		$count = $this->vendor_mapper->count();
		
		return array($results, $count);
	}
	public function getPublicSearchField(){
		return Vendor_object::getPublicSearchField();
	}
	public function loadVendor($vendor_id){
		return $this->vendor_mapper->Find($vendor_id);
	}
	private function buildSearchCriteria($condition = array()){
		$criteria = array();
		foreach ($this->public_search_field as $field){
			if(!empty($condition[$field])){
				$criteria[$field] = $condition[$field];
			}
		}
		return $criteria;
	}
	public function createVendor($data){
		if(!isset($data['vendorName']) || !isset($data['vendorCode']) || !isset($data['businessType'])){
			return false;
		}
		$vendor = new Vendor_object(null, $data['vendorName'], strtoupper($data['vendorCode']), $data['businessType']);
 		return ObjectMap_helper::getMapper('Vendor_object')->save($vendor);
		 
	}
}